package cn.action.admin;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;

import cn.action.BaseAction;
import cn.beans.Administrator;
import cn.utils.MD5;
import cn.utils.PageModel;

@SuppressWarnings("unchecked")
public class AdminAction extends BaseAction {

	private Administrator admin;
	private String adminId;
	private String password;
	private String rePassword;
	private String errorinfo;
	private String keywords;
	private int currentPage;
	private int pageSize;
	private PageModel pm = new PageModel();
	
	/**
	 * 通过ajax检测用户名是否已存在
	 * @throws Exception
	 */
	public void checkUsername() throws Exception{
		if(null != adminId && !"".equals(adminId.trim())){
			boolean tag = this.checkExist(adminId);
			if(tag){
				this.sendMsgAjax("ok", null);
			}else{
				this.sendMsgAjax("exist", null);
			}
		}
	}
	
	/**
	 * 添加管理员
	 * @throws Exception
	 */
	public void addAdmin() throws Exception{
		try{
			
			String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
			if(null == sadminId){
				this.sendMsgAjax("login", null);
			}else if(!"admin".equals(sadminId)){
				this.sendMsgAjax("powerRight", null);
			}else{
				
				if(null == admin || null == admin.getUsername() || "".equals(admin.getUsername().trim())){
					this.sendMsgAjax("username", null);
				}else if(null == password || "".equals(password.trim()) 
					|| null == rePassword || "".equals(rePassword.trim())){
					this.sendMsgAjax("password", null);
				}else if(!password.equals(rePassword)){
					this.sendMsgAjax("notTheSame", null);
				}else{
					if(checkExist(admin.getUsername())){
						String md5Password = MD5.getMD5ofStr(password);
						admin.setPassword(md5Password);
						commonservice.saveDemo(admin);
						
						this.sendMsgAjax("success", null);
					}else{
						this.sendMsgAjax("exist", null);
					}
					
				}
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 删除管理员
	 */
	public void deleteAdmin() throws Exception{
		try{
			String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
			if(null == sadminId){
				this.sendMsgAjax("login", null);
			}else if(!"admin".equals(sadminId)){
				this.sendMsgAjax("powerRight", null);
			}else{
				String[] ids = adminId.split(",");
				int length = ids.length;
				for(int i=0;i<length;i++){
					Administrator administrator = commonservice.findById(ids[i], new Administrator());
					if(null != administrator){
						commonservice.deleteDemo(administrator, null);
					}
				}
				this.sendMsgAjax("success", null);
			}
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 修改管理员信息
	 * @throws Exception
	 */
	public void updateAdmin() throws Exception{
		try{
			
			String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
			if(null == sadminId){
				this.sendMsgAjax("login", null);
			}else if(!"admin".equals(sadminId) && !sadminId.equals(adminId)){
				this.sendMsgAjax("powerRight", null);
			}else{
				
				if(null == adminId || "".equals(adminId.trim())){
					this.sendMsgAjax("username", null);
				}else{
					Administrator administrator = commonservice.findById(adminId, new Administrator());
					if(null == administrator){
						this.sendMsgAjax("notExist", null);
					}else{
						
						if(null != password && !"".equals(password)){
							String md5Password = MD5.getMD5ofStr(password);
							administrator.setPassword(md5Password);
						}
						
						administrator.setEmail(admin.getEmail());
						administrator.setMobile(admin.getMobile());
						administrator.setMsn(admin.getMsn());
						administrator.setName(admin.getName());
						administrator.setQq(admin.getQq());
						administrator.setTelephone(admin.getTelephone());
						
						commonservice.updateDemo(administrator);
						
						if(sadminId.equals(adminId)){
							this.sendMsgAjax("self", null);
						}else{
							this.sendMsgAjax("success", null);
						}
					}
				}
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 通过ID查询管理员
	 * @return
	 */
	public String adminDetail(){
		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		if(null == sadminId){
			errorinfo = "timeover";
			return LOGIN;
		}else if("admin".equals(sadminId) || sadminId.equals(adminId)){
			admin = commonservice.findById(adminId, new Administrator());
		}
		return SUCCESS;
	}
	
	/**
	 * 列表显示管理员
	 * @return
	 */
	public String listAdmin(){
		
		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		if(null == sadminId){
			errorinfo = "Time Over";
			return LOGIN;
		}else if("admin".equals(sadminId)){
			pm.setCurrentPage(currentPage);
			pm.setPageSize(pageSize);
			
			List listOne = new ArrayList();
			String[] propertyName = { "username" };
			String[] propertyCon = { "!=" };
			Object[] propertyValue = { "admin" };
			String[] condition = null;
			listOne.add(propertyName);
			listOne.add(propertyCon);
			listOne.add(propertyValue);
			listOne.add(condition);

			List listTwo = new ArrayList();
			String[] propertyNameQuery = { "username","name","telephone","mobile","email","qq","msn" };
			String[] propertyConQuery = { "like","like","like","like","like","like","like" };
			Object[] propertyValueQuery = { keywords,keywords,keywords,keywords,keywords,keywords,keywords };
			String[] conditionQuery = { "OR","OR","OR","OR","OR","OR" };
			listTwo.add(propertyNameQuery);
			listTwo.add(propertyConQuery);
			listTwo.add(propertyValueQuery);
			listTwo.add(conditionQuery);

			String[] orderPropertyName = { "username" };
			String[] orderValue = { "ASC" };

			pm = commonservice.findDataByPage(pm, "Administrator", listOne, "AND", listTwo, null, null, null, null, orderPropertyName, orderValue);

		}
		return SUCCESS;
		
	}
	
	/**
	 * 修改个人信息
	 * @return
	 */
	public String editSelf(){
		adminId = (String)ActionContext.getContext().getSession().get("adminId");
		if(null == adminId){
			return LOGIN;
		}else{
			return SUCCESS;
		}
	}

	public Administrator getAdmin() {
		return admin;
	}

	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}

	public String getAdminId() {
		return adminId;
	}

	public void setAdminId(String adminId) {
		this.adminId = adminId;
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRePassword() {
		return rePassword;
	}

	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}

	public String getErrorinfo() {
		return errorinfo;
	}

	public void setErrorinfo(String errorinfo) {
		this.errorinfo = errorinfo;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}

	/**
	 * 检测用户是否存在
	 * @param username
	 * @return 用户存在则返回false,不存在则返回true
	 */
	private boolean checkExist(String username){
		boolean tag = true;
		Administrator administrator = commonservice.findById(username, new Administrator());
		if(null != administrator){
			tag = false;
		}
		
		return tag;
	}
}
